// index.js
const express = require("express");
const axios = require("axios");
const cheerio = require("cheerio");
const pretty = require("pretty");

const app = express();

let currentId = "5708267";
let isEnd = false;
let errId = "";
let errNum = 0;

const url =
  "https://fundf10.eastmoney.com/FundArchivesDatas.aspx?type=jjcc&code=003095&topline=20"; //基金股票持仓代码接口
const PORT = 9909;
// ("https://fundf10.eastmoney.com/FundArchivesDatas.aspx?type=jjcc&code=003095&topline=20");
// ("http://push2.eastmoney.com/api/qt/ulist.np/get?fltt=2&secids=1.000001,0.399001&invt=2&fields=f2,f3,f4,f6,f12,f14,f104,f105,f106&ut=267f9ad526dbe6b0262ab19316f5a25b&cb=jQuery183008437562674843258_1690765158366&_=1690765158377");

app.get("/detail", (req, res) => {
  // res.send("Hello World!");
  // console.log("hhh");
  // console.log(req.body, req.query);
  getDetail(req.query.stockCode, res);
});

app.get("/fundList", (req, res) => {
  // res.send("Hello World!");
  // console.log("hhh");
  console.log(req.body, req.query);
  getFundList(req.query.fundCode, res);
});

app.get("/", (req, res) => {
  getData(req.query.fundCode, res);
});

app.listen(PORT, () => {
  console.log(`Example app listening on port ${PORT}`);
  // 获取数据，可以写在路由中通过接口形式调用
  //   getData(currentId)
});

function resolveHtml(content) {
  const table = content(".boxitem table tbody");
  // console.log(table[0].children);
  let rate = [];
  let total = [];
  for (let i = 0; i < Array.from(table[0]?.children || [])?.length; i++) {
    try {
      rate.push(table[0].children[i].children[6].children[0].data);
      total.push(table[0].children[i].children[8].children[0].data);
    } catch (error) {
      console.log(error);
    }
  }
  return {
    rate,
    total,
  };
}

function getData(code, res) {
  axios
    .get(
      `https://fundf10.eastmoney.com/FundArchivesDatas.aspx?type=jjcc&code=${code}&topline=20`
    )
    .then(async (data) => {
      const $ = await cheerio.load(data.data);
      // console.log($('#gpdmList').html()); // 股票代码
      // console.log(pretty($.html()));
      // resolveHtml($)
      res.send({
        data: $("#gpdmList").html(),
        html: $("#gpdmList").html(),
        rate: resolveHtml($).rate,
        total: resolveHtml($).total,
      });
    });
}

function getDetail(codeStr, res) {
  axios
    .get(
      `https://push2.eastmoney.com/api/qt/ulist.np/get?fltt=2&invt=2&fields=f2,f3,f4,f5,f12,f14,f9,f6&secids=${codeStr}&_=${Date.now()}`
    )
    .then(async (data) => {
      // const $ = await cheerio.load(data.data);
      // console.log(pretty($.html()));
      // res.send(pretty($.html()))
      // console.log(data);
      res.send(data.data);
    });
}

function getFundList(code, res) {
  axios
    .get(
      `https://fundsuggest.eastmoney.com/FundSearch/api/FundSearchAPI.ashx?m=1&key=${encodeURI(
        code
      )}&_=${Date.now()}`
    )
    .then(async (data) => {
      // const $ = await cheerio.load(data.data);
      // console.log(pretty($.html()));
      // res.send(pretty($.html()))
      // console.log(data);
      res.send(data.data.Datas);
    });
}
